package cn.iocoder.yudao.module.shop.service.order.handler;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory;
import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum;
import cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderDisplayModeEnum;
import cn.iocoder.yudao.module.pay.api.order.PayOrderApi;
import cn.iocoder.yudao.module.pay.api.order.dto.PayOrderCreateReqDTO;
import cn.iocoder.yudao.module.pay.controller.admin.order.vo.PayOrderSubmitReqVO;
import cn.iocoder.yudao.module.pay.controller.admin.order.vo.PayOrderSubmitRespVO;
import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletTransactionDO;
import cn.iocoder.yudao.module.pay.enums.wallet.PayWalletBizTypeEnum;
import cn.iocoder.yudao.module.pay.framework.pay.core.WalletPayClient;
import cn.iocoder.yudao.module.pay.service.order.PayOrderService;
import cn.iocoder.yudao.module.pay.service.wallet.PayWalletService;
import cn.iocoder.yudao.module.shop.dal.dataobject.order.ShopOrderDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.order.ShopOrderItemDO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class WalletPayShopOrderHandler implements ShopOrderHandler{

    @Resource
    private PayWalletService payWalletService;

    /**
     * 支付订单后
     *
     * @param order 订单
     */
    public void afterPayOrder(ShopOrderDO order, List<ShopOrderItemDO> orderItems) {
        if(order.getWalletCharge()){
            payWalletService.shopOrderPay(order.getMemberId(), UserTypeEnum.MEMBER.getValue(), order.getId(), order.getPayPrice(), PayWalletBizTypeEnum.SHOP_PAYMENT);
        }
    }

    @Override
    public void rollbackOrder(ShopOrderDO order, List<ShopOrderItemDO> shopOrderItems){
        if(!order.getWalletCharge()){
            return;
        }

        payWalletService.shopOrderRollback(order.getMemberId(), UserTypeEnum.MEMBER.getValue(), order.getId(), order.getPayPrice(), PayWalletBizTypeEnum.SHOP_ROLLBACK);
    }
}
